<template>
  <DataList 
  :cate="cate" 
  :cols="cols"
  :name="name"
  :excelHeader="excelHeader"
  :groupCheck = "groupCheck" 
 ref="datalist"
  skipExcelNumber="3" />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get开设专业} from  "@/api/开设专业"
import { addZyzgzs, deleteZyzgzs, updateZyzgzs, getZyzgzs, bulkAddZyzgzs } from "@/api/职业资格证书.js";
import {findComponent} from "@/utils/ui";
import linq from "linq"
import { Promise, resolve } from 'q';
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A7-3-1职业资格证书表",
      name: "7.3.1 职业资格证书",
      excelHeader:{B3:"专业所属系部代码",C3:"专业所属系部名称",D3:"专业代码",E3:"专业名称（全称）",F3:"专业方向代码",G3:"专业方向名称（全称）",H3:"学生获取的符合专业面向职业资格证书",H4:"名称(全称)",I4:"等级",J4:"人数",K4:"发证机构",L4:"鉴定地点"},
     
      cols: [
        { label: "专业所属系部代码", prop: "BMBH" },
        { label: "专业所属系部名称", prop: "BMMC" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "专业方向代码", prop: "ZYFXDM" },
        { label: "专业方向名称(全称)", prop: "ZYFXMC" },
        { label: "学生获取的符合专业面向职业资格证书" ,data:[
            {label:"名称(全称)",prop:"ZYZGZS_MC"},{ label: "等级", prop: "XZ_ZYZGZS_DJ" },
        { label: "人数", prop: "XSZYZG_SR" },{ label: "发证机构(全称)", prop: "ZYZGZS_FZDW" },
        { label: "鉴定地点", prop: "XZ_ZYZGZS_JDDD" }
        ]}
      ],
      groupCheck:[{data:["ZYZGZS_MC","XZ_ZYZGZS_DJ","XSZYZG_SR","ZYZGZS_FZDW","XZ_ZYZGZS_JDDD"],desc:'请完善学生获取的符合专业面向职业资格证书的输入'}]
    };
  },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
         sums[0]="合计";
         sums[6]="系部数:"+linq.from(list).distinct("$.BMBH").count()
         sums[8]="专业数:"+linq.from(list).distinct("$.ZYDM").count()
         sums[10]="方向数:"+linq.from(list).distinct("$.ZYFXDM").count()
         sums[12]="证书种类:"+linq.from(list).distinct("$.ZYZGZS_MC").count()
         sums[14]="总人数:"+linq.from(list).sum("$.XSZYZG_SR")
         sums[15]="单位数:"+linq.from(list).distinct("$.ZYZGZS_FZDW").count()
         sums[17]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
         sums[17]+= ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
         sums[17] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
 
       return sums;
      //
    },
    async  onDlgCreated(model) {
          var dd = findComponent(this,"ZYFXMC");
          var ZYFXMC = linq.from( this.$root.开设专业).distinct("$.ZYFXMC").toArray();
        
          dd.$attrs.items = ZYFXMC.map(item => {
            return { label: item.ZYFXMC, value: item.ZYFXMC};
          });
    },
    async  bulkAdd(array){
       await  bulkAddZyzgzs({Zyzgzs:array});
    },
    check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
        
        
          var err =  linq.from(array).select("$.ZYDM+','+$.ZYFXDM").except(linq.from( this.$root.开设专业).select("$.ZYDM+','+$.ZYFXDM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM+','+e.ZYFXDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"开设专业表不存在对应的专业代码与专业方向代码组合合");
          //把错误的教师姓名替代掉
          err =  linq.from(array).select("$.ZYDM+','+$.ZYMC+','+$.ZYFXDM+','+$.ZYFXMC").except(linq.from(this.$root.开设专业).select("$.ZYDM+','+$.ZYMC+','+$.ZYFXDM+','+$.ZYFXMC").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM+','+e.ZYMC+','+e.ZYFXDM+','+e.ZYFXMC) !==-1).
          forEach(e=>{
            var d = linq.from(this.$root.开设专业).firstOrDefault(item=>item.ZYDM == e.ZYDM && item.ZYFXDM == e.ZYFXDM);
            if(d){
              e.ZYMC = d.ZYMC;
              e.ZYFXMC = d.ZYFXMC;
              e.BMMC = d.BMMC;
              e.BMBH = d.BMBH;
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
    onChange(model, field, value) {
      //
      if (field == "ZYFXMC") {
        
        var temp =  linq.from( this.$root.开设专业).first(item=>item.ZYFXMC==value);
        model.BMBH = temp.BMBH;
        model.BMMC = temp.BMMC;
        model.ZYMC = temp.ZYMC;
        model.ZYDM = temp.ZYDM;
        
        model.ZYFXDM = temp.ZYFXDM;
        
      }
      //
    },
    
    async fetchData() {
     
      var datas = await getZyzgzs(this.$root.user.XXID,this.$root.user.CJPC);
      return datas;
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      
          console.log(JSON.stringify(temp));
          var model = await addZyzgzs(temp);
          resolve(model);
      })
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateZyzgzs(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
         await deleteZyzgzs(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>